#include "Base\Engine.h"
#include "UI\Controls\ControlFactory.h"
#include "UI\Controls\Control.h"

///////////////////////////////////////////////////////////
//        ///  //////  /        ///  ///////  ///        //
//  //////  /  //////  /  //////  /  ///////  /  //////////
//        ///  //////  /        ///  ///////  /  //////////
//  /////////  //////  /  //////  /  ///////  /  //////////
//  ///////////      ///        ///        /  ///        //
///////////////////////////////////////////////////////////

ShRp::UI::Control::Control(Type::E _Type)
	: m_Type(_Type)
{
}

bool ShRp::UI::Control::StaticInit()
{
	// Create and register the wndClass.
	{
		WNDCLASS wndClass;

		wndClass.style          = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
		wndClass.lpfnWndProc    = ControlMgr::HandleHWNDMessage;
		wndClass.cbClsExtra     = 0;
		wndClass.cbWndExtra     = 0;
		wndClass.hInstance      = NULL;
		wndClass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
		wndClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
		wndClass.hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
		wndClass.lpszMenuName   = NULL;
		wndClass.lpszClassName  = TEXT("BorderlessWindowClassName");

		RegisterClass(&wndClass);
	}
	return true;
}

bool ShRp::UI::Control::Init(int2 _Pos, int2 _Size)
{
	this->Move(_Pos);
	this->Resize(_Size);

	ShowWindow(m_HWND, SW_SHOW);

	return true;
}

void ShRp::UI::Control::Terminate()
{
}

void ShRp::UI::Control::Move( int2 _NewPos )
{
	m_Pos = _NewPos;
	SetWindowPos(m_HWND, NULL, m_Pos.x, m_Pos.y, m_Size.x, m_Size.y, NULL);
}

void ShRp::UI::Control::Resize( int2 _NewSize )
{
	m_Size = _NewSize;
	SetWindowPos(m_HWND, NULL, m_Pos.x, m_Pos.y, m_Size.x, m_Size.y, NULL);
}

void ShRp::UI::Control::AddChild( Widget* _Widget )
{
	m_Children.push_back(_Widget);
}

////////////////////////////////////////////////////////////////
//      ///      ///  /  //////  ///    ///          /        //
//  ////  /  ////  /  /  //////  /  ////  /////  /////  ////////
//      ///      ///  ///  //  ///        /////  /////      ////
//  ///////  //  ///  ///  //  ///  ////  /////  /////  ////////
//  ///////  ////  /  /////  /////  ////  /////  /////        //
////////////////////////////////////////////////////////////////

void ShRp::UI::Control::CreateHWND()
{
	m_HWND = CreateWindow(	TEXT("BorderlessWindowClassName"),	// window class name
							TEXT(""),							// window caption
							WS_POPUP,							// window style
							0,									// initial x position
							0,									// initial y position
							300,								// initial x size
							200,								// initial y size
							NULL,								// parent window handle
							NULL,								// window menu handle
							NULL,								// program instance handle
							NULL);								// creation parameters
}

void ShRp::UI::Control::HandleMessage( WindowsMessage::E _Message, ... )
{
	va_list vaList;
	va_start(vaList, _Message);
	switch(_Message)
	{
	case WindowsMessage::Paint:
		{
			this->Paint();
		}
		break;
	case WindowsMessage::Destroy:
		{
		}
		break;
	case WindowsMessage::MouseMove:
		{
			this->MouseMove();
			for(WidgetVector::iterator widgetIt = m_Children.begin(); widgetIt != m_Children.end(); ++widgetIt)
			{
				(*widgetIt)->MouseMove();
			}
		}
		break;
	case WindowsMessage::MouseDown:
		{
			const MouseButton::E btn = va_arg(vaList, MouseButton::E);
			this->MouseDown(btn);
			for(WidgetVector::iterator widgetIt = m_Children.begin(); widgetIt != m_Children.end(); ++widgetIt)
			{
				(*widgetIt)->MouseDown(btn);
			}
		}
		break;
	case WindowsMessage::MouseUp:
		{
			const MouseButton::E btn = va_arg(vaList, MouseButton::E);
			this->MouseUp(btn);
			for(WidgetVector::iterator widgetIt = m_Children.begin(); widgetIt != m_Children.end(); ++widgetIt)
			{
				(*widgetIt)->MouseUp(btn);
			}
		}
		break;
	case WindowsMessage::KeyDown:
		{
			const Key::En key = va_arg(vaList, Key::En);
			this->KeyDown(key);
			for(WidgetVector::iterator widgetIt = m_Children.begin(); widgetIt != m_Children.end(); ++widgetIt)
			{
				(*widgetIt)->KeyDown(key);
			}
		}
		break;
	case WindowsMessage::KeyRepeat:
		{
			const Key::En key = va_arg(vaList, Key::En);
			this->KeyRepeat(key);
			for(WidgetVector::iterator widgetIt = m_Children.begin(); widgetIt != m_Children.end(); ++widgetIt)
			{
				(*widgetIt)->KeyRepeat(key);
			}
		}
		break;
	case WindowsMessage::KeyUp:
		{
			const Key::En key = va_arg(vaList, Key::En);
			this->KeyUp(key);
			for(WidgetVector::iterator widgetIt = m_Children.begin(); widgetIt != m_Children.end(); ++widgetIt)
			{
				(*widgetIt)->KeyUp(key);
			}
		}
		break;
	}
	va_end(vaList);
}